<?php

namespace app\site\controller\store;

use app\common\exception\MiException;
use app\model\store\Goods;
use app\model\store\GoodsCate;
use app\site\controller\AController;
use app\site\controller\traits\CurdTrait;
use app\site\service\SiteReqService;
use app\site\service\store\GoodsCateService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Request;
use think\response\Json;

class GoodsCateController extends AController
{

    use CurdTrait;

    private GoodsCateService $service;

    protected function initialize()
    {
        $this->service = GoodsCateService::instance();
    }

    /**
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function index (): Json
    {
        $service = GoodsCateService::instance();
        $list = SiteReqService::instance()->setService($service)->setSelect()->getList();
        $tree = $service->childrenTree($list);
        return jsonSuccess([
            'items' => $tree,
        ]);
    }


    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws MiException
     */
    public function del()
    {
        // 如果分类存在下级分类，不能删除
        return SiteReqService::instance()->setService($this->service)->delete()->getResponse();
    }
}
